<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Crypt;
use DB;

class UserController extends Controller
{
    //add
    public function add()
    {

    	return view('admin.user.add', ['title' => '用户添加']);
    }
    //insert
    public function insert(Request $request)
    {
    	// dd($request -> repassword);
        // dd($request -> all());
    	$this -> validate($request, [
    		'name' => 'required|min:6|max:18',
    		// 'email' => 'required|email',
    		'password' => 'required',
    		'repassword' => 'required|same:password'
    		],[
    		'name.required' => '用户名不能为空',
    		'name.min' => '用户名最少6位',
    		'name.max' => '用户名最大18位',
    		'email.required' => '邮箱不能为空',
    		'email.email' => '邮箱格式不正确',
    		'password.required' => '密码不能为空',
    		'repassword.required' => '确认密码不能为空',
    		"repassword.same" => '两次密码不一致'
    		]);

    		$data = $request -> except('_token', 'repassword');

    		//处理密码
    		$data['password'] = Crypt::encrypt($data['password']);
    		// $p = Crypt::decrypt($data['password']);
    		// dd($p);
    		//处理token字段
    		$data['remember_token'] = str_random(50);

    		//处理头像
    		if($request -> hasFile('pic'))
    		{
    			if($request -> file('pic') -> isValid())
    			{
    				$suffix = $request -> file('pic') -> 
    					getClientOriginalExtension();
    				$fileName = time().mt_rand(10000,99999).'.'.$suffix;
    				//移动文件
    				$move = $request -> file('pic') -> move('./uploads/yhtx', $fileName);
    			}
    				if($move)
    				{
    					$data['pic'] = $fileName;
    				}else
    				{
	    				$data['pic'] = 'default.jpg';
                    }
            }else
            {
                $data['pic'] = 'default.jpg';
			}
                // var_dump($data);

    			$res = DB::table('users') -> insert($data);
                // dd($res);
    			if($res)
    			{
    				return redirect('/admin/user/index');
    			}else
    			{
    				return back();
    			}


    		
    		}
    

    public function index(Request $request)
    {
        // 查询数据
        // dd($request -> all());
        $data= DB::table('users') ->where('name', 'like', '%'.$request -> keywords.'%') -> paginate($request -> input('num', 10));
    	// dd($data -> all());
        return view('admin.user.index', ['title' => '用户列表','data' => $data, 'request' => $request -> all()]);
    }


    public function ajaxStatus(Request $request)
    {
        $id = $request -> all()['id'];
        // echo $id;
        //通过数据库查询构造器value方法来查询一个字段的信息
        $select = DB::table('users') -> where('id', $id) -> value('status');
        // echo $select;
        if($select == 0)
        {
            $res = DB::table('users') -> where('id', $id) -> update(['status' => 1]);
            // return $res;
            if($res)
            {
                return 1;
            }else{
                return 2;
            }

        }elseif($select == 1)
        {
            $res = DB::table('users') -> where('id', $id) -> update(['status' => 0]);
            if($res)
            {
                return 0;
            }else
            {
                return 2;
            }
        }
    }

    public function ajaxAuth(Request $request)
    {
        $id = $request -> all()['id'];
        $auth = DB::table('users') -> where('id', $id) -> value('auth');
        // var_dump($auth);die();
        if($auth == 0)
        {
            $data = DB::table('users') -> where('id', $id) -> update(['auth' => 1]);
            if($data)
            {
                return 1;
            }else
            {
                return 0;
            }
        }elseif ($auth == 1) {
            $data = DB::table('users') -> where('id', $id) -> update(['auth' => 0]);
            if($data)
            {
                return 2;
            }else
            {
                return 0;
            }
            }

        
    }

    public function edit($id)
    {
        $data = DB::table('users') -> where('id', $id) -> first();
        return view('admin/user/edit', ['title' => '用户编辑', 'data' => $data]);

    }

    //update
    public function update(Request $request)
    {
        dd($request);
        // dd($data);
        //获取原来图片
        $oldPic = DB::table('users') -> where('id',$request -> id) -> first() -> pic;
        // dd($oldPic);
        // dd($request -> hasFile('pic'));
        if($request -> hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                $fileName = time().mt_rand(10000,99999).'.'.$suffix;
                //移动文件
                $move = $request -> file('pic') -> move('/uploads/yhtx',$fileName);

                if($move)
                {
                    $data['pic'] = $fileName;
                    //删除原来图片
                    unlink('/uploads/yhtx/'.$oldPic);
                    
                }
            }
        }
        //执行修改
        $res = DB::table('users') -> where('id', $request -> id) -> update($data);

        if($res)
        {
            return redirect('/admin/user/index');
        }else
        {
            return back();
        }
    }

    public function delete($id)
    {
        //获取原来图片
        $oldPic = DB::table('users') -> where('id', $id) ->first() -> pic;
        $res = DB::table('users') -> delete($id);
        if($res)
        {
            // 删除图片
            if(file_exists('./uploads/yhtx/'.$oldPic))
            {
                unlink('./uploads/yhtx/'.$oldPic);
            }
            return redirect('admin/user/index');
        }else
        {
            return back();
        }
    }
}
